package com.ymttest.business.data;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonArray;
import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.couponmanage.service.SellerCreateBatchCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.service.CreateSellerLiveCall;
import com.ymatou.iapi.productquery.parameter.ProductDetail.ProductInDetailBean;
import com.ymatou.iapi.productquery.service.GetProductInfoByProductIdCall;
import com.ymatou.iapi.selleractivity.service.ApplyActivityCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductQueryCallService;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class DataLive {

	public static void main(String[] args) throws Exception {
		// 产线数据准备
		// 商品数据 来自app的sqlserver integratedproduct库
		// select sProductId,iUserId from ymt_products where sproduct =
		// 'ceshiwupai' and ibrandid = 11634 and daddtime<GetDate() and daddtime
		// >2017/10/24 and iuserid=21158951
		ArrayList<String> sellerlist = TestDataManager.getLines("data");
		//遍历19 个 sstest 商户 
		for (int sellercount = 0; sellercount < sellerlist.size(); sellercount++) {
			String[] sellerinfo = sellerlist.get(sellercount).split(" ");
			Integer userid = Integer.valueOf(sellerinfo[0]);
			String uname = sellerinfo[1];
			JsonArray jo = LiveRoomCallService.SellerlivelistCall(userid);
			//查询后关闭直播
			if (jo != null) {
//			{"Id":292997,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#a5XDABQG3","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-05-31 21:22:18","EndTime":"2020-05-31 22:12:18","Longitude":100.2026,"Latitude":31.5129,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZu50QevdpIJJnC1batRqE/iw==","HistoryPlayUrl":"","CreateTime":"2020-05-31 22:24:00","UpdateTime":"2020-05-31 23:02:18","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":326697,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aRMMNDUGL","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-15 23:17:08","EndTime":"2020-08-03 23:20:03","Longitude":100.5859,"Latitude":31.1304,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuSRiOPVG0AFU/QbqJqkP9DA==","HistoryPlayUrl":"","CreateTime":"2020-08-03 23:17:06","UpdateTime":"2020-08-03 23:20:03","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":326492,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aOUQLDUGS","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-15 22:25:19","EndTime":"2020-08-03 22:30:05","Longitude":100.4260,"Latitude":31.8237,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuJm3GozG9h6hujxwYYyqJGA==","HistoryPlayUrl":"","CreateTime":"2020-08-03 22:25:16","UpdateTime":"2020-08-03 22:30:05","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":326059,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aWWWWBUGM","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-14 23:09:34","EndTime":"2020-08-02 23:10:05","Longitude":100.6331,"Latitude":31.8314,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuG8QPAMbPQWmVcUCy+ZAGsw==","HistoryPlayUrl":"","CreateTime":"2020-08-02 23:09:32","UpdateTime":"2020-08-02 23:10:05","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":325865,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#a2PCVBUGJ","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-14 22:24:40","EndTime":"2020-08-02 22:30:04","Longitude":100.6186,"Latitude":31.8546,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuOldLuuosIl47Fi2TOWzLQw==","HistoryPlayUrl":"","CreateTime":"2020-08-02 22:24:38","UpdateTime":"2020-08-02 22:30:04","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":325432,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aJELJAUGS","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-13 23:09:16","EndTime":"2020-08-01 23:10:03","Longitude":100.6534,"Latitude":31.1748,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuDLCYuCcouBLKecfUHc0Buw==","HistoryPlayUrl":"","CreateTime":"2020-08-01 23:09:13","UpdateTime":"2020-08-01 23:10:03","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":325243,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aIBYHAUGZ","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-13 22:24:34","EndTime":"2020-08-01 22:25:06","Longitude":100.7857,"Latitude":31.4977,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZujmTG+seJtiAHvRS4FNPwHw==","HistoryPlayUrl":"","CreateTime":"2020-08-01 22:24:31","UpdateTime":"2020-08-01 22:25:06","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":324808,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aHHE26TGH","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-12 23:04:02","EndTime":"2020-07-31 23:05:03","Longitude":100.3831,"Latitude":31.5176,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZuMMeNNRH0bJpQau+y/ex6QQ==","HistoryPlayUrl":"","CreateTime":"2020-07-31 23:03:59","UpdateTime":"2020-07-31 23:05:03","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":324631,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aR2SY6TGR","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-12 22:24:05","EndTime":"2020-07-31 22:25:04","Longitude":100.4598,"Latitude":31.8650,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZue68If0FikgX8NwRN0Ib35Q==","HistoryPlayUrl":"","CreateTime":"2020-07-31 22:24:02","UpdateTime":"2020-07-31 22:25:04","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false},{"Id":324184,"UserId":21158951,"SellerName":"sstest2","LiveStatus":8,"LiveStatusText":"已关闭","GroupId":"@TGS#aDV3D5TGQ","LiveTitle":"AT测试直播-今天的主题是?","LiveCover":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","StartTime":"2020-04-11 23:04:05","EndTime":"2020-07-30 23:05:14","Longitude":100.4971,"Latitude":31.4944,"LiveAddress":"live地址","PreVideoUrl":"http://v2.ymatou.com/uploads/20171024/f99192ff0f712310e7a39f43edb38f11.mp4","PreVideoPic":"http://s2.ymatou.com/login/5aebbf5a6893bd825441a68a60b42b51.jpg","PreVideoLength":1500,"PreVideoWidth":200,"PreVideoHeight":100,"ViewNum":0,"OnLineNum":0,"LikeNum":0,"PushUrl":"","PlayUrl":"nrmmavKS+34pYI0R1l9XBnB+Jfh9Pmj/Ct7cDYqqkhAvGPOUHOSBciJVNgjafUZusxjuqCwwS3zfJHkJ7jl1Tg==","HistoryPlayUrl":"","CreateTime":"2020-07-30 23:04:03","UpdateTime":"2020-07-30 23:05:14","StopReason":"","LiveProductNum":0,"LivePush":0,"LiveDesc":null,"LiveType":0,"StreamId":null,"FlvPlayUrl":null,"LiveCategoryId":1,"AuditState":0,"LiveCategoryName":null,"LiveNoticeId":0,"LiveNoticeContent":null,"StartPlayTime":0,"LiveActivityId":0,"LiveActivityName":null,"LiveActivityState":-1,"RealViewNum":0,"RealOnlineNum":0,"TopInfo":null,"IsSchedule":false}]
				for (int i = 0; i < jo.size(); i++) {
					if (jo.get(i).getAsJsonObject().get("LiveStatus").getAsInt() < 4) {
						LiveRoomCallService.CloseSellerLive(jo.get(i).getAsJsonObject().get("Id").getAsInt(), userid,
								8);
						LiveRoomCallService.CloseSellerLive(jo.get(i).getAsJsonObject().get("Id").getAsInt(), userid,
								7);
						LiveRoomCallService.CloseSellerLive(jo.get(i).getAsJsonObject().get("Id").getAsInt(), userid,
								9);
					}
					;
				}
			}
			//创建直播
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initOnlineCreatesellerliveBean(userid, uname);
			CreateSellerLiveCall createsellerliveCall = LiveRoomCallService.CreateLive(initcreatebean);
			int liveid = createsellerliveCall.getJsonObject().get("Data").getAsJsonObject().get("LiveId").getAsInt();
			List<String> ProductIdList = new ArrayList<>();
			ArrayList<String> list = TestDataManager.getLines("stgcreateproduct.log");
			//加点商品进直播
			int count = 0;
			TestDataManager.appendFileToLine("stglive.txt", "sellerid,liveid,pid,cid");
			for (String line : list) {
				String[] ss = line.split(":");
				//直播最多100个商品
				if (ss[0].equals(userid.toString()) && count < 100) {
					ProductIdList.add(ss[1]);
					count++;
					TestDataManager.appendFileToLine("stglive.txt", userid + "," + liveid + "," + ss[1]+ "," + ss[2]);
				}
			}
			LiveRoomCallService.AddProductsToLive(liveid, userid, ProductIdList);
//			Thread.sleep(60000 * 2);
			LiveRoomCallService.SellerLiveOnline(liveid, userid, false);
			LiveRoomCallService.SetLiveProductTop(liveid, ProductIdList.get(0));
		}
		//合并文件
		DataMerge.LeftMerge("stglive.txt", "pid", "sellertoken.txt", "sellerid", "temp2.txt");
	}
}
